<?php
Globals::requireTable('User');

class UserModel extends Config
{
	protected $table;
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->table = new UserTable($config);
	}
	
	public function login($username, $password)
	{
		if (empty($username) || empty($password))
		{
			$this->message = '用户名或密码为空。';
			return false;
		}
		
		$row = $this->table->getRow(array('username' => $username, 'password' => md5($password)));
		
		if (!$row)
		{
			$this->message = '用户名或密码错误。';
			return false;
		}
		
		$data = array(
			'prelogintime'	=> $row['logintime'],
			'preloginip'	=> $row['loginip'],
			'logintime'		=> time(),
			'loginip'		=> Globals::getClientIp(true),
			'logintimes'	=> $row['logintimes'] + 1
		);
		
		$result = $this->table->update($data, $row['id']);
		return $result ? array_merge($row, $data) : false;
	}
}
